﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Media.Imaging;

namespace RandomSelect
{
	public partial class MainWindow : Window
	{
		Random ran = new Random();
		bool IsLooping = false;
		DispatcherTimer LoopTimer = new DispatcherTimer();
		DispatcherTimer ConfigTimer = new DispatcherTimer();
		public bool doubleIndex = false;
		private int[] arr1;
		private int[] arr2;
		private int nowShowNum = 0;
		static ConfigSetting config = new ConfigSetting();
		Request myRequest = new Request();
		string NextSelect = "null";
		string NoSelect = "null";
		readonly Dictionary<string, string> argsDic = config.ReadConfig("./config.dll");

		//Window的初始化
		public MainWindow()
		{
			InitializeComponent();
			//初始化
			radioButton_SingleIndexMode.IsChecked = true;
			SetSingleIndex(true);
			SetDoubleIndex(false);
			textBox_SingleIndex1.Text = argsDic["SingleIndex1"];
			textBox_SingleIndex2.Text = argsDic["SingleIndex2"];
			textBox_SingleIndexEndle.Text = "";
			textBox_DoubleIndex1.Text = argsDic["DoubleIndex1"];
			textBox_DoubleIndex2.Text = argsDic["DoubleIndex2"];
			textBox_DoubleIndex3.Text = argsDic["DoubleIndex3"];
			textBox_DoubleIndex4.Text = argsDic["DoubleIndex4"];
			textBox_DoubleIndexEndle1.Text = "组";
			textBox_DoubleIndexEndle2.Text = "位";
			checkBox_isFill0.IsChecked = false;
			checkBox_WallpaperEnable.IsChecked = argsDic["Wallpaper"] == "True"?true:false;

			//连接数据库
			myRequest.SetUrl("http://tinywebdb.appinventor.space/api");
			//定时获取Config
			ConfigTimer.Interval = System.TimeSpan.FromMilliseconds(2500);//每2.5s刷新一次
			ConfigTimer.Tick += ConfigTimer_Tick;//绑定定时器事件
			ConfigTimer.Start();
			GetConfig();

			//应用必应每日壁纸
			if(checkBox_WallpaperEnable.IsChecked == true)SetImage();
		}


		//窗体交互逻辑
		private void RadioButton_SingleIndexMode_Unchecked(object sender, RoutedEventArgs e)
		{
			SetSingleIndex(false);
		}

		private void RadioButton_DoubleIndexMode_Unchecked(object sender, RoutedEventArgs e)
		{
			SetDoubleIndex(false);
		}

		private void RadioButton_SingleIndexMode_Checked(object sender, RoutedEventArgs e)
		{
			SetSingleIndex(true);
		}

		private void RadioButton_DoubleIndexMode_Checked(object sender, RoutedEventArgs e)
		{
			SetDoubleIndex(true);
		}

		private void SetSingleIndex(bool enable)
		{
			switch (enable)
			{
				case true:
					textBox_SingleIndex1.IsEnabled = true;
					textBox_SingleIndex2.IsEnabled = true;
					label1.IsEnabled = true;
					label2.IsEnabled = true;
					textBox_SingleIndexEndle.IsEnabled = true;
					break;

				case false:
					textBox_SingleIndex1.IsEnabled = false;
					textBox_SingleIndex2.IsEnabled = false;
					label1.IsEnabled = false;
					label2.IsEnabled = false;
					textBox_SingleIndexEndle.IsEnabled = false;
					break;

				default:
					break;
			}
		}

		private void SetDoubleIndex(bool enable)
		{
			switch (enable)
			{
				case true:
					textBox_DoubleIndex1.IsEnabled = true;
					textBox_DoubleIndex2.IsEnabled = true;
					textBox_DoubleIndex3.IsEnabled = true;
					textBox_DoubleIndex4.IsEnabled = true;
					label3.IsEnabled = true;
					label4.IsEnabled = true;
					label5.IsEnabled = true;
					label6.IsEnabled = true;
					textBox_DoubleIndexEndle1.IsEnabled = true;
					textBox_DoubleIndexEndle2.IsEnabled = true;
					break;

				case false:
					textBox_DoubleIndex1.IsEnabled = false;
					textBox_DoubleIndex2.IsEnabled = false;
					textBox_DoubleIndex3.IsEnabled = false;
					textBox_DoubleIndex4.IsEnabled = false;
					label3.IsEnabled = false;
					label4.IsEnabled = false;
					label5.IsEnabled = false;
					label6.IsEnabled = false;
					textBox_DoubleIndexEndle1.IsEnabled = false;
					textBox_DoubleIndexEndle2.IsEnabled = false;
					break;

				default:
					break;
			}
		}
		//校验输入
		private void TextBox_SingleIndex2_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (textBox_SingleIndex2.Text != "")
			{
				int asc = (int)textBox_SingleIndex2.Text[textBox_SingleIndex2.Text.Length - 1];
				if (asc < 48 || asc > 57) textBox_SingleIndex2.Text = textBox_SingleIndex2.Text.Substring(0, textBox_SingleIndex2.Text.Length - 1);
			}
		}

		private void TextBox_SingleIndex1_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (textBox_SingleIndex1.Text != "")
			{
				int asc = (int)textBox_SingleIndex1.Text[textBox_SingleIndex1.Text.Length - 1];
				if (asc < 48 || asc > 57) textBox_SingleIndex1.Text = textBox_SingleIndex1.Text.Substring(0, textBox_SingleIndex1.Text.Length - 1);
			}
		}

		private void TextBox_DoubleIndex1_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (textBox_DoubleIndex1.Text != "")
			{
				int asc = (int)textBox_DoubleIndex1.Text[textBox_DoubleIndex1.Text.Length - 1];
				if (asc < 48 || asc > 57) textBox_DoubleIndex1.Text = textBox_DoubleIndex1.Text.Substring(0, textBox_DoubleIndex1.Text.Length - 1);
			}
		}

		private void TextBox_DoubleIndex2_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (textBox_DoubleIndex2.Text != "")
			{
				int asc = (int)textBox_DoubleIndex2.Text[textBox_DoubleIndex2.Text.Length - 1];
				if (asc < 48 || asc > 57) textBox_DoubleIndex2.Text = textBox_DoubleIndex2.Text.Substring(0, textBox_DoubleIndex2.Text.Length - 1);
			}
		}

		private void TextBox_DoubleIndex3_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (textBox_DoubleIndex3.Text != "")
			{
				int asc = (int)textBox_DoubleIndex3.Text[textBox_DoubleIndex3.Text.Length - 1];
				if (asc < 48 || asc > 57) textBox_DoubleIndex3.Text = textBox_DoubleIndex3.Text.Substring(0, textBox_DoubleIndex3.Text.Length - 1);
			}
		}

		private void TextBox_DoubleIndex4_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (textBox_DoubleIndex4.Text != "")
			{
				int asc = (int)textBox_DoubleIndex4.Text[textBox_DoubleIndex4.Text.Length - 1];
				if (asc < 48 || asc > 57) textBox_DoubleIndex4.Text = textBox_DoubleIndex4.Text.Substring(0, textBox_DoubleIndex4.Text.Length - 1);
			}
		}
		//关闭程序前写入当前配置
		private void Window_Closed(object sender, EventArgs e)
		{
			string configValue = "SingleIndex1=" + textBox_SingleIndex1.Text + "\nSingleIndex2=" + textBox_SingleIndex2.Text + "\nDoubleIndex1=" + textBox_DoubleIndex1.Text + "\nDoubleIndex2=" + textBox_DoubleIndex2.Text + "\nDoubleIndex3=" + textBox_DoubleIndex3.Text + "\nDoubleIndex4=" + textBox_DoubleIndex4.Text + "\nWallpaper="+ checkBox_WallpaperEnable.IsChecked.ToString();
			config.WriteConfig("./config.dll", configValue);
		}
		//壁纸相关
		private void CheckBox_WallpaperEnable_Checked(object sender, RoutedEventArgs e)
		{
			SetImage();
		}

		private void CheckBox_WallpaperEnable_Unchecked(object sender, RoutedEventArgs e)
		{
			image.Visibility = Visibility.Hidden;
			textBlock_imageInf.Text = "";
		}

		//抽号按钮事件
		private void Button_begin_Click(object sender, RoutedEventArgs e)
		{
			textBox_SingleIndex1.Text = (textBox_SingleIndex1.Text == "") ? argsDic["SingleIndex1"] : textBox_SingleIndex1.Text;
			textBox_SingleIndex2.Text = (textBox_SingleIndex2.Text == "") ? argsDic["SingleIndex2"] : textBox_SingleIndex2.Text;
			textBox_DoubleIndex1.Text = (textBox_DoubleIndex1.Text == "") ? argsDic["DoubleIndex1"] : textBox_DoubleIndex1.Text;
			textBox_DoubleIndex2.Text = (textBox_DoubleIndex2.Text == "") ? argsDic["DoubleIndex2"] : textBox_DoubleIndex2.Text;
			textBox_DoubleIndex3.Text = (textBox_DoubleIndex3.Text == "") ? argsDic["DoubleIndex3"] : textBox_DoubleIndex3.Text;
			textBox_DoubleIndex4.Text = (textBox_DoubleIndex4.Text == "") ? argsDic["DoubleIndex4"] : textBox_DoubleIndex4.Text;
			LoopTimer.Interval = TimeSpan.FromMilliseconds(20);
			IsLooping = IsLooping ? false : true;
			if (IsLooping)
			{
				GetConfig();
				LoopTimer.Start();
				button_begin.Content = "停止抽号";
			}
			else
			{
				LoopTimer.Stop();
				GetConfig();
				button_begin.Content = "开始抽号";
				if (NextSelect != "null" && NextSelect != "0")
				{
					textBlock_NUMBER.Text = NextSelect + textBox_SingleIndexEndle.Text;
					WriteConfig("NextSelect", "0");
				}
				if (NoSelect != "null" && NoSelect != "0" && NoSelect == nowShowNum.ToString())
				{
					string rd = ran.Next(int.Parse(textBox_SingleIndex1.Text), int.Parse(textBox_SingleIndex2.Text)).ToString();
					while (rd == NoSelect)
					{
						rd = ran.Next(int.Parse(textBox_SingleIndex1.Text), int.Parse(textBox_SingleIndex2.Text)).ToString();
					}
					textBlock_NUMBER.Text = rd + textBox_SingleIndexEndle.Text;
				}
			}
			//高位补零操作
			if (radioButton_DoubleIndexMode.IsChecked == true)
			{
				doubleIndex = true;
				DoubleIndexSelect(int.Parse(textBox_DoubleIndex1.Text), int.Parse(textBox_DoubleIndex2.Text) + 1, int.Parse(textBox_DoubleIndex3.Text), int.Parse(textBox_DoubleIndex4.Text) + 1);
			}
			if (radioButton_SingleIndexMode.IsChecked == true)
			{
				doubleIndex = false;
				SingleIndexSelect(int.Parse(textBox_SingleIndex1.Text), int.Parse(textBox_SingleIndex2.Text) + 1);
			}
		}
		

		//窗体交互逻辑涉及的方法
		async private void SetImage()
		{
			string uri = "";
			uri = await myRequest.GetRequest("https://cn.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1&mkt=zh-CN");
			image.Source = new BitmapImage(new Uri(uri.Split('~')[0]));
			textBlock_imageInf.Text = uri.Split('~')[1];
			image.Visibility = Visibility.Visible;
		}

		async private void GetConfig()
		{
			try
			{
				NextSelect = await myRequest.PostRequest("NextSelect", "get");
				NoSelect = await myRequest.PostRequest("NoSelect", "get");
			}
			catch
			{
			}
		}

		async private void WriteConfig(string key, string val)
		{
			await myRequest.PostRequest(key, "update", val);
		}

		private void ConfigTimer_Tick(object sender, EventArgs e)
		{
			GetConfig();
		}

		//抽号操作逻辑
		private void SingleIndexSelect(int argMinIndex, int argMaxIndex)
		{
			int[] arr1 = new int[Math.Abs(argMaxIndex - argMinIndex)];
			for (int i = 0; i < arr1.Length; i++)
			{
				arr1[i] = i + Math.Min(argMinIndex, argMaxIndex);
			}

			SetArr(arr1, arr1);
			LoopTimer.Tick += LoopTimer_Tick;
		}

		private void DoubleIndexSelect(int argMinIndex1, int argMaxIndex1, int argMinIndex2, int argMaxIndex2)
		{
			int[] arr1 = new int[Math.Abs(argMaxIndex1 - argMinIndex1)];
			int[] arr2 = new int[Math.Abs(argMaxIndex2 - argMinIndex2)];
			for (int i = 0; i < arr1.Length; i++)
			{
				arr1[i] = i + Math.Min(argMinIndex1, argMaxIndex1);
			}
			for (int i = 0; i < arr2.Length; i++)
			{
				arr2[i] = i + Math.Min(argMinIndex2, argMaxIndex2);
			}

			SetArr(arr1, arr2);
			LoopTimer.Tick += LoopTimer_Tick;
		}

		private void LoopTimer_Tick(object sender, EventArgs e)
		{
			int showNum1 = arr1[ran.Next(0, arr1.Length)];
			int showNum2 = arr2[ran.Next(0, arr2.Length)];
			string showStr1;
			string showStr2;

			if (checkBox_isFill0.IsChecked == true)
			{
				showStr1 = (showNum1 < 10) ? ("0" + showNum1.ToString()) : showNum1.ToString();
				showStr2 = (showNum2 < 10) ? ("0" + showNum2.ToString()) : showNum2.ToString();
			}
			else
			{
				showStr1 = showNum1.ToString();
				showStr2 = showNum2.ToString();
			}

			if (doubleIndex)
			{
				textBlock_NUMBER.Text = showStr1 + textBox_DoubleIndexEndle1.Text + showStr2 + textBox_DoubleIndexEndle2.Text;
			}
			else
			{
				textBlock_NUMBER.Text = showStr1 + textBox_SingleIndexEndle.Text;
				nowShowNum = showNum1;
			}
		}

		private void SetMode(bool argMode)
		{
			doubleIndex = argMode;
		}

		private void SetArr(int[] argArr1, int[] argArr2)
		{
			arr1 = argArr1;
			arr2 = argArr2;
		}

	}
}
